﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BKIDS;
using BKIDS.CDBNames;
using BKIUS;
using IP.Core.IPCommon;
using IP.Core.IPUserService;
using IP.Core.IPData;
using System.Data;

public partial class QuanLyNhanSu_F204_QuanLyHocVien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            load_2_cbo_loaihocvien();
            load_2_cbo_tendoituong();
            XoaTrang();
            load_2_grv_hocvien("");
        }
    }

    #region Members
    US_DM_HOC_VIEN m_us_hocvien;
    DS_DM_HOC_VIEN m_ds_hocvien;
    #endregion

    #region Public Interfaces
    public string Mapping_hoten(string ip_str_id_doituong)
    {
        try
        {
            decimal v_i_id_doituong = CIPConvert.ToDecimal(ip_str_id_doituong);
            US_DM_DOI_TUONG m_us_dm_doituong = new US_DM_DOI_TUONG(v_i_id_doituong);           
            return CIPConvert.ToStr(m_us_dm_doituong.strHO_VA_TEN_DEM + " " + m_us_dm_doituong.strTEN);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    public string Mapping_laoihocvien(string ip_str_id_loaihocvien)
    {
        try
        {
            decimal v_i_id_loaiHV = CIPConvert.ToDecimal(ip_str_id_loaihocvien);
            US_DM_LOAI_HOC_VIEN m_us_dm_loaihocvien = new US_DM_LOAI_HOC_VIEN(v_i_id_loaiHV);
            return CIPConvert.ToStr(m_us_dm_loaihocvien.strTEN_LOAI_HV);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    
    #endregion

    #region Private Methods
    private void load_2_cbo_tendoituong()
    {
        US_DM_DOI_TUONG v_us_dm_doi_tuong = new US_DM_DOI_TUONG();
        DS_DM_DOI_TUONG v_ds_dm_doi_tuong = new DS_DM_DOI_TUONG();
        v_us_dm_doi_tuong.FillDataset(v_ds_dm_doi_tuong);

        for (int v_i = 0; v_i < v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows.Count; v_i++)
        {
            m_cbo_tenhocvien.Items.Add(new ListItem(CIPConvert.ToStr(v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows[v_i][DM_DOI_TUONG.HO_VA_TEN_DEM]).Trim() + " " + CIPConvert.ToStr(v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows[v_i][DM_DOI_TUONG.TEN]).Trim(), CIPConvert.ToStr(v_ds_dm_doi_tuong.DM_DOI_TUONG.Rows[v_i][DM_DOI_TUONG.ID])));
        }
    }
    private void load_2_cbo_loaihocvien()
    {
        try
        {
            US_DM_LOAI_HOC_VIEN v_us_loaihocvien = new US_DM_LOAI_HOC_VIEN();
            DS_DM_LOAI_HOC_VIEN v_ds_loaihocvien = new DS_DM_LOAI_HOC_VIEN();
            v_us_loaihocvien.FillDataset(v_ds_loaihocvien);
            m_cbo_loaihocvhien.DataSource = v_ds_loaihocvien.DM_LOAI_HOC_VIEN;
            m_cbo_loaihocvhien.DataTextField = DM_LOAI_HOC_VIEN.TEN_LOAI_HV;
            m_cbo_loaihocvhien.DataValueField = DM_LOAI_HOC_VIEN.ID;
            m_cbo_loaihocvhien.DataBind();
        }
        catch (Exception v_e)
        {
            m_lbl_lois.Text = v_e.Message;
            m_lbl_lois.Visible = true;
        }
        
    }

    private void load_2_grv_hocvien(string ip_tendangnhap)
    {
        m_us_hocvien = new US_DM_HOC_VIEN();
        m_ds_hocvien = new DS_DM_HOC_VIEN();        
        try
        {
            m_ds_hocvien.Clear();
            // Đổ dữ liệu từ US vào DS
            if (ip_tendangnhap == "")
                m_us_hocvien.FillDataset(m_ds_hocvien);
            else
                m_us_hocvien.FillDataset(m_ds_hocvien, "WHERE TEN_DANG_NHAP LIKE '%" + ip_tendangnhap + "%'");
            // Nếu dữ liệu trống
            if (m_ds_hocvien.DM_HOC_VIEN.Rows.Count == 0)
            {
                m_lbl_thong_bao.Text = "Chưa có lịch sử nào cho thành viên này";
            }
            m_grv_dm_hocvien.DataSource = m_ds_hocvien.DM_HOC_VIEN;
            m_grv_dm_hocvien.DataBind();
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_thong_bao.Text = v_e.Message;
            m_lbl_thong_bao.Visible = true;

        }
    }

    private void XoaTrang()
    {
        m_cbo_tenhocvien.Text = "";
        m_cbo_loaihocvhien.Text = "";
        m_txt_madoituong.Text = "";
        m_txt_matkhau.Text = "";
        m_txt_tendangnhap.Text = "";
        m_lbl_lois.Visible = false;
        m_lbl_thong_bao.Visible = false;
        m_btt_capnhat.ToolTip = "";
        m_btt_capnhat.Enabled = false;
        m_btt_taomoi.Enabled = true;
    }

    private void from_2_object(US_DM_HOC_VIEN ob_us_hocvien)
    {
        try
        {
            ob_us_hocvien.dcID = CIPConvert.ToDecimal(m_cbo_tenhocvien.SelectedValue);
            ob_us_hocvien.strTEN_DANG_NHAP = CIPConvert.ToStr(m_txt_tendangnhap.Text);
            ob_us_hocvien.strMAT_KHAU = CIPConvert.ToStr(m_txt_matkhau.Text);
            ob_us_hocvien.dcID_LOAI_HV = CIPConvert.ToDecimal(m_cbo_loaihocvhien.SelectedValue);
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_lois.Text = v_e.Message;
            m_lbl_lois.Visible = true;
        }
    }

    private void delete_row_thanh_toan(int ip_i_id_hoc_vien)
    {
        // Lấy được ID thanh tóan
        decimal v_dc_id_hoc_vien = CIPConvert.ToDecimal(m_grv_dm_hocvien.DataKeys[ip_i_id_hoc_vien].Value);
        m_us_hocvien.dcID = v_dc_id_hoc_vien;
        // Xóa GD_THANH_TOAN
        m_us_hocvien.DeleteByID(v_dc_id_hoc_vien);
        m_lbl_thong_bao.Text = "Xóa bản ghi thành công";
        // Load lại dữ liệu
        load_2_grv_hocvien("");
    }

    private void load_data_2_us_by_id_and_show_on_form(int ip_i_hoc_vien_selected)
    {
        try
        {
            decimal v_dc_id_hoc_vien = CIPConvert.ToDecimal(m_grv_dm_hocvien.DataKeys[ip_i_hoc_vien_selected].Value);
            m_btt_capnhat.ToolTip = CIPConvert.ToStr(v_dc_id_hoc_vien);
            US_DM_HOC_VIEN v_us_dm_hoc_vien = new US_DM_HOC_VIEN(v_dc_id_hoc_vien);            
            // Load data to form 
            m_cbo_tenhocvien.SelectedValue = CIPConvert.ToStr(v_us_dm_hoc_vien.dcID);
            m_txt_tendangnhap.Text = CIPConvert.ToStr(v_us_dm_hoc_vien.strTEN_DANG_NHAP);
            m_txt_matkhau.Text = CIPConvert.ToStr(v_us_dm_hoc_vien.strMAT_KHAU);
            m_cbo_loaihocvhien.SelectedValue = CIPConvert.ToStr(v_us_dm_hoc_vien.dcID_LOAI_HV);            
        }
        catch (Exception v_e)
        {

            throw v_e;
        }

    }
    #endregion

    #region Events
    protected void m_btt_locdulieu_Click(object sender, EventArgs e)
    {
        try
        {
            load_2_grv_hocvien(m_txt_loc_madoituong.Text.Trim());
        }
        catch (Exception v_e)
        {
            m_lbl_lois.Text = v_e.Message;
            m_lbl_lois.Visible = true;
        }
    }

    protected void m_btt_xoatrang_Click(object sender, EventArgs e)
    {
        XoaTrang();
    }

    protected void  m_btt_taomoi_Click(object sender, EventArgs e)
    {
        try
        {
            if (m_cbo_tenhocvien.SelectedValue != null && m_txt_tendangnhap.Text != "" && m_txt_matkhau.Text != "" && m_cbo_loaihocvhien.SelectedValue!= null)
            {
                if (m_btt_capnhat.ToolTip != "") return;
                from_2_object(m_us_hocvien);
                m_us_hocvien.Insert();
                load_2_grv_hocvien("");                
                XoaTrang();
            }
            else
            {
                //nhớ using Ip.Common
                m_lbl_lois.Text = "Bạn chưa điềm đủ các thông tin cần thiết";
                m_lbl_lois.Visible = true;
            }
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_lois.Text = v_e.Message;
            m_lbl_lois.Visible = true;
        }
    }

    protected void  m_cbo_tenhocvien_SelectedIndexChanged(object sender, EventArgs e)
    {
        US_DM_DOI_TUONG v_us_dm_doi_tuong = new US_DM_DOI_TUONG(CIPConvert.ToDecimal(m_cbo_tenhocvien.SelectedValue));
        m_txt_madoituong.Text = v_us_dm_doi_tuong.strMA_DOI_TUONG; 
    }

    protected void  m_btt_capnhat_Click(object sender, EventArgs e)
    {
        try
        {
            if (m_cbo_tenhocvien.SelectedValue != null && m_txt_tendangnhap.Text != "" && m_txt_matkhau.Text != "" && m_cbo_loaihocvhien.SelectedValue!= null)
            {
                if (m_btt_capnhat.ToolTip == "") {
                    m_lbl_lois.Text = "Bạn chưa chọn một dòng để cập nhật";
                    m_lbl_lois.Visible = true;
                    return;
                }
                from_2_object(m_us_hocvien);
                m_us_hocvien.Update();
                load_2_grv_hocvien("");                
                XoaTrang();
            }
            else
            {
                //nhớ using Ip.Common
                m_lbl_lois.Text = "Bạn chưa điềm đủ các thông tin cần thiết";
                m_lbl_lois.Visible = true;
            }
        }
        catch (Exception v_e)
        {
            //nhớ using Ip.Common
            m_lbl_lois.Text = v_e.Message;
            m_lbl_lois.Visible = true;
        }
    }

    protected void m_grv_dm_hocvien_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            m_grv_dm_hocvien.PageIndex = e.NewPageIndex;
            load_2_grv_hocvien("");
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    
    protected void m_grv_dm_hocvien_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {
            m_btt_taomoi.Enabled = false;
            m_btt_capnhat.Enabled = true;
            load_data_2_us_by_id_and_show_on_form(e.NewSelectedIndex);
        }
        catch (Exception V_e)
        {
            CSystemLog_301.ExceptionHandle(this, V_e);
        }
    }
    protected void m_grv_dm_hocvien_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            delete_row_thanh_toan(e.RowIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }    
    #endregion


}